package com.cloudcompare.diff.utils;

import com.cloudcompare.diff.compare.JavapWrapper;

public class Dissasembler {
	/**
	 * Classpath
	 * @param paramString
	 * @return
	 */
	public static String fixClassPath(String paramString) {
		String str1 = System.getProperty("java.class.path");
		String str2 = System.getProperty("path.separator");

		String str3 = str2 + paramString + str2;
		while (true) {
			int i = str1.indexOf(str3);
			if (i < 0)
				break;
			str1 = str1.substring(0, i) + str2
					+ str1.substring(i + str3.length()) + str1;
		}

		str3 = paramString + str2;
		if (str1.startsWith(str3))
			str1 = str1.substring(str3.length());
		str3 = str2 + paramString;
		if (str1.endsWith(str3))
			return str1;
		if (str1.endsWith(paramString))
			return str1;
		return str1 + str2 + paramString;
	}
	/**
	 * Decompila
	 * @param className
	 * @param classpath
	 * @return
	 */
	public static String disassemble(String className, String classpath) {
		return disassembleJAVAP(GenericUtils.entryNameToClassName(className), classpath);
	}

	/**
	 * Decompila
	 * @param className
	 * @param classpath
	 * @return
	 */
	public static String disassembleJAVAP(String className, String classpath) {
		className = GenericUtils.removeDotClass(className);

		String str = "";
		try {
			str = JavapWrapper.run(className, classpath);
		} catch (Exception localException) {
			localException.printStackTrace();
		}
		if (str.startsWith("No source")) {
			str = str.substring("No source".length());
		} else if (str.startsWith("Compiled from")) {
			int i = str.indexOf("\n");
			if (i >= 0)
				str = str.substring(i + "\n".length());
		}
		return str;
	}
}
